package com.bos.service.impl;

import com.bos.data.model.BosBehaviorModel;
import com.bos.data.model.BosParamModel;
import com.bos.data.model.BosUserModel;
import com.bos.data.model.SalesModel;
import com.bos.data.repositories.BehaviorRespository;
import com.bos.data.repositories.jpa.BosBehaviorJPARepository;
import com.bos.mapper.Bos_behaviorMapper;
import com.bos.service.Bos_behaviorService;
import com.bos.common.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service("bos_behaviorServiceImpl")
public class Bos_behaviorServiceImpl implements Bos_behaviorService {
    private Logger logger = LoggerFactory.getLogger(Bos_behaviorServiceImpl.class);
    @Autowired
    Bos_behaviorMapper behaMapper;
    //将SpringdateJpa注入到service层
    @Autowired
    BosBehaviorJPARepository bosBehaviorJPARepository;

    @Autowired
	private BehaviorRespository behaviorRespository;
    //1.查询出bos_behavior中的数据
    @Override
    public PageData findBehavior(int pageSize, int pageIndex) {
		return behaviorRespository.findSales(pageSize,pageIndex);
    }

    //2将数据插入到bos_behavior中
    @Override
    public int insertBehavior(BosBehaviorModel behavior) {
        // TODO Auto-generated method stub
        bosBehaviorJPARepository.save(behavior);
        return 0;
    }

    //3根据id删除表中的数据
    @Transactional
    @Override
    public int deleteById(int bid) {
        return behaMapper.deleteById(bid);
    }

    //4.根据id查询出对应·的数据
    @Override
    public BosBehaviorModel findByBehavior(int bid) {
        return behaMapper.findByBehavior(bid);
    }

    //5.根据id修改数据
    @Override
    public int updateByID(BosBehaviorModel behavior) {
        return behaMapper.updateByID(behavior);
    }

    //6.根据typeName查询出数据
    @Override
    public List<BosParamModel> findBos_params(String typeName) {
        return behaMapper.findBos_params(typeName);
    }

    @Override
    public List<BosBehaviorModel> findBehaviors() {
        // TODO Auto-generated method stub
        return behaMapper.findBehavior();
    }
    @Override
    public List<BosUserModel> findUsers() {
        // TODO Auto-generated method stub
        return behaMapper.findUsers();
    }

    @Transactional
    @Override
    public String copyBehaviour(int id){
        try {
            BosBehaviorModel model=bosBehaviorJPARepository.findOne(id);
            BosBehaviorModel newModel=new BosBehaviorModel();
            BeanUtils.copyProperties(model,newModel);
            newModel.setId(null);
            newModel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            bosBehaviorJPARepository.save(newModel);
            return model.getBname();
        }catch (Exception e){
            logger.error("copy行为数据失败:id{}",id,e);
            return "";
        }
    }
}